//
//  ProductDetailsLabels.m
//  quickBatchManager
//
//  Created by jack on 15/9/30.
//  Copyright © 2015年 jack. All rights reserved.
//

#import "ProductDetailsLabels.h"

@implementation ProductDetailsLabels
{
    uint rowHeight;
    uint cellWidth;
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

- (id)initWithFrame:(CGRect)frame andColumnsWidths:(NSArray*)columns
{
    self = [super initWithFrame:frame];
    if (self) {
        numRows = 0;
        self->columnsWidths = columns;
        self->dy = 0;
        self->numRows = 0;
        self->rowHeight = 35;
        self->cellWidth = [columns[0] integerValue];
    }
    
   
    return self;
}




-(void)addTopView:(NSString *)title
{
    self->dy = 35;
    UILabel *topLabel  = [[UILabel alloc] initWithFrame:CGRectMake(2, 0, cellWidth*[columnsWidths count]-4, rowHeight)];
    topLabel.backgroundColor = [UIColor colorWithRed:140/255.0 green:100/255.0 blue:158/255.0 alpha:1.0];
    topLabel.text = [NSString stringWithFormat:@"    %@",title];
    topLabel.textColor = [UIColor whiteColor];
    [self addSubview:topLabel];
}

-(void)addBootomView:(NSString *)title
{
    UILabel *bootomLabel  = [[UILabel alloc] initWithFrame:CGRectMake(2,rowHeight*numRows+rowHeight-4,  cellWidth*[columnsWidths count]-3, rowHeight)];
    bootomLabel.backgroundColor = [UIColor colorWithRed:224/255.0 green:196/255.0 blue:222/255.0 alpha:1.0];
    bootomLabel.text = [NSString stringWithFormat:@"    %@",title];
    bootomLabel.textColor = [UIColor colorWithRed:135/255.0 green:97/255.0 blue:165/255.0 alpha:1.0];
    [self addSubview:bootomLabel];
    
    self.frame = CGRectMake(self.frame.origin.x, self.frame.origin.y, cellWidth*[columnsWidths count], self.frame.size.height+rowHeight);
    
    self.layer.borderWidth = 3;
    self.layer.cornerRadius = 3.0;
    self.layer.borderColor = [UIColor colorWithRed:177/255.0 green:156/255.0 blue:196/255.0 alpha:1.0].CGColor;
}


- (void)addRecord: (NSArray*)record {
    if(record.count != self->columnsWidths.count){
        NSLog(@"!!! Number of items does not match number of columns. !!!");
        return;
    }
    
    
    uint dx = 2;
    
    NSMutableArray* labels = [[NSMutableArray alloc] init];
    
    //CREATE THE ITEMS/COLUMNS OF THE ROW
    for(uint i=0; i<record.count; i++){
        float colWidth = [[self->columnsWidths objectAtIndex:i] floatValue];	//colwidth as given at setup
        CGRect rect = CGRectMake(dx, dy, colWidth-2, rowHeight);
        
        //ADJUST X FOR BORDER OVERLAPPING BETWEEN COLUMNS
        if(i>0){
            rect.origin.x -= i;
        }
        
        //--------------------------------------------
        
        UILabel* col1 = [[UILabel alloc] init];
        [col1.layer setBorderColor:[[UIColor colorWithRed:187/255.0 green:140/255.0 blue:88/255.0 alpha:1.0] CGColor]];  //线的颜色
        [col1.layer setBorderWidth:1.0];
        col1.font = [UIFont fontWithName:@"Helvetica" size:12.0];
        col1.textColor = [UIColor colorWithRed:135/255.0 green:97/255.0 blue:165/255.0 alpha:1.0];
        col1.frame = rect;
        
        
        //SET LEFT RIGHT MARGINS & ALIGNMENT FOR THE LABEL
        NSMutableParagraphStyle *style =  [[NSParagraphStyle defaultParagraphStyle] mutableCopy];
        style.alignment = NSTextAlignmentNatural;
        style.headIndent = 10;
        style.firstLineHeadIndent = 10.0;
        style.tailIndent = -10.0;
        
        
        //SPECIAL TREATMENT FOR THE FIRST ROW
        if(self->numRows == 0){
            style.alignment = NSTextAlignmentCenter;
            col1.backgroundColor = [UIColor colorWithRed:220/255.0 green:192/255.0 blue:234/255.0 alpha:1.0];
        }else
        {
            style.alignment = NSTextAlignmentCenter;
            col1.backgroundColor =  [UIColor colorWithRed:230/255.0 green:215/255.0 blue:251/255.0 alpha:1.0];
        }
        
        
        NSAttributedString *attrText = [[NSAttributedString alloc] initWithString:[record objectAtIndex:i] attributes:@{ NSParagraphStyleAttributeName : style}];
        
        
        col1.lineBreakMode = NSLineBreakByCharWrapping;
        col1.numberOfLines = 0;
        col1.attributedText = attrText;
        [col1 sizeToFit];
        
        
        //USED TO FIND HEIGHT OF LONGEST LABEL
        CGFloat h = col1.frame.size.height + 15;
        if(h > rowHeight){
            rowHeight = h;
        }
        
        //MAKE THE LABEL WIDTH SAME AS COLUMN'S WIDTH
        rect.size.width = colWidth;
        col1.frame = rect;
        
        [labels addObject:col1];
        
        //USED FOR SETTING THE NEXT COLUMN X POSITION
        dx += colWidth;
    }
    
    
    //MAKE ALL THE LABELS OF SAME HEIGHT AND THEN ADD TO VIEW
    for(uint i=0; i<labels.count; i++){
        UILabel* tempLabel = (UILabel*)[labels objectAtIndex:i];
        CGRect tempRect = tempLabel.frame;
        tempRect.size.height = rowHeight;
        tempLabel.frame = tempRect;
        [self addSubview:tempLabel];
    }
    
    self->numRows++;
    
    
    //ADJUST y FOR BORDER OVERLAPPING BETWEEN ROWS
    self->dy += rowHeight-1;
    
    
    //RESIZE THE MAIN VIEW TO FIT THE ROWS
    CGRect tempRect = self.frame;
    tempRect.size.height = dy;
    tempRect.size.width = cellWidth*[columnsWidths count];
    self.frame = tempRect;
    
    self.layer.borderWidth = 3;
    self.layer.cornerRadius = 3.0;
    self.layer.borderColor = [UIColor colorWithRed:187/255.0 green:140/255.0 blue:88/255.0 alpha:1.0].CGColor;
}



@end
